using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Barebones.Components;
using Microsoft.Xna.Framework;

namespace _2dgame.Components
{
    class CopsWalkComponent : EntityComponent, Barebones.Framework.IUpdateable
    {
        float x_, y_, angle_ = 0;
        Vector2 walk_;
        bool firstWay_ = true, negativeX_, negativeY_, negative_, horizontalWalk_ = false, verticalWalk_ = false;
        Vector2 rootPosition_, targetPosition_;
        const float SPEED = 0.01f;

        public CopsWalkComponent(Vector2 deplacement, Vector2 root)
        {
            targetPosition_ = root + deplacement;
            walk_ = deplacement;
            walk_.Normalize();
            rootPosition_ = root;
            x_ = rootPosition_.X;
            y_ = rootPosition_.Y;   

            if (deplacement.X != 0)
            {
                horizontalWalk_ = true;
                negative_ = (rootPosition_.X - targetPosition_.X) < 0 ? false : true;
            }
            else
            {
                verticalWalk_ = true;
                angle_ = (float)(Math.PI / 2.0);
                negative_ = (rootPosition_.Y - targetPosition_.Y) < 0 ? false : true;
            }
        }

        public override IEnumerable<Barebones.Dependencies.IDependency> GetDependencies()
        {
            yield break;
        }

        public void Update(float dt)
        {
            if (firstWay_)
                x_ += SPEED;
            else
                x_ -= SPEED;

         /*   if (!firstWay_ && horizontalWalk_)
                angle_ = (float)(Math.PI);
            else if (firstWay_ && horizontalWalk_)
                angle_ = 0;

            if (!firstWay_ && verticalWalk_)
                angle_ = (float)(3.0*Math.PI/2.0);
            else if (firstWay_ && verticalWalk_)
                angle_ = (float)(Math.PI / 2.0);
            */
            if (horizontalWalk_ && !negative_ && x_ > targetPosition_.X)
            {
                firstWay_ = false;
                x_ = targetPosition_.X;

            }
            else if (horizontalWalk_ && negative_ && x_ < targetPosition_.X)
            {
                firstWay_ = false;
                x_ = targetPosition_.X;
            }
            else if (horizontalWalk_ && !negative_ && x_ < rootPosition_.X)
            {
                firstWay_ = true;
                x_ = rootPosition_.X;
            }
            else if (horizontalWalk_ && negative_ && x_ > rootPosition_.X)
            {
                firstWay_ = true;
                x_ = rootPosition_.X;
            }

            if (verticalWalk_ && !negative_ && x_ > targetPosition_.Y)
                firstWay_ = false;
            else if (verticalWalk_ && negative_ && x_ < targetPosition_.Y)
                firstWay_ = false;
            else if (verticalWalk_ && !negative_ && x_ < rootPosition_.Y)
                firstWay_ = true;
            else if (verticalWalk_ && negative_ && x_ > rootPosition_.Y)
                firstWay_ = true;

            Owner.Transform = Matrix.CreateTranslation(new Vector3(x_, y_, 0))*Matrix.CreateRotationZ(angle_);
        }
    }
}
